<template>
  <div>
    <van-nav-bar
  title="黑马头条-登录"
  left-arrow

  placeholder
/>
<div>
  <van-form @submit="onSubmit">
  <van-field
    v-model="user.mobile"
    name="mobile"
    label="手机号"
    placeholder="请输入手机号"
    :rules="[{ required: true, message: '请填写正确用户名',pattern: /^1[3-9]\d{9}$/ }]"
  />
  <van-field
    v-model="user.code"
    type="password"
    name="code"
    label="密码"
    placeholder="请输入密码"
    :rules="[{ required: true, message: '请填写正确密码',pattern: /^\d{6}$/ }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit" :loading="isLoading" loading-text="正在登录中" :disabled="isLoading">提交</van-button>
  </div>
</van-form>
</div>
  </div>
</template>

<script>
import { loginApi } from '@/api'
import { Notify } from 'vant'
import { setToken } from '@/utils/token'
export default {
// eslint-disable-next-line vue/multi-word-component-names
  name: 'Login',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      isLoading: false
    }
  },
  methods: {
    async onSubmit (value) {
      // console.log(value)
      this.isLoading = true
      try {
        const res = await loginApi(value)
        console.log(res)
        Notify({ type: 'success', message: '成功' })
        setToken(res.data.data.token)
        this.$router.replace('/layout')
      } catch (error) {
        console.log(error)
        Notify({ type: 'danger', message: '失败' })
      }
      this.isLoading = false
    }
  }
}
</script>

<style scoped lang="less">
.van-nav-bar{
  background-color: #007bff;
}
/deep/
.van-nav-bar__title{
color: white;
}
/deep/
.van-nav-bar__text{
  color: #eee;
}
</style>
